package com.kayer.ai.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
@RequestMapping("/ai")
@RequiredArgsConstructor //只生成final类型和使用@NonNull注解的字段的构造器
public class ChatController {

    private final ChatClient client;//客户端

    @RequestMapping(value = "/chat",produces = "text/html;charset=utf-8")
    //要流式输出，就需要使用spring5特性：Flux<>接收
    public Flux<String> chat(String prompt,String chatId){//用户输入提示词，即问题
        return client.prompt(prompt) //传入user提示词
                .advisors(a -> a.param(ChatMemory.CONVERSATION_ID,chatId))//设置记忆
                .stream()
                .content();//返回响应内容
    }
}
